package com.example.dialogdemo.receiver;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.dialogdemo.R;

public class NFBroadcastReceiver extends BroadcastReceiver {

    private final String TAG = "NFBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG,intent.getAction());

        showNotification(context);
    }

    private void showNotification(Context context){
        String CHANNEL_ID = "my_channel_03";
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){        //Android 8.0适配
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "my_channel_03_name",
                    NotificationManager.IMPORTANCE_LOW);

            notificationManager.createNotificationChannel(channel);
        }
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context,CHANNEL_ID);

        builder
                .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                .setContentTitle("Notification后台通知") //指定通知栏的标题内容
                .setContentText("Notification后台通知的正文内容")//通知的正文内容
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setColor(Color.rgb(255,0,0))
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                .setNumber(1)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.clean))
                .setSmallIcon(R.drawable.demonstration);//通知显示的小图标，只能用alpha图层的图片进行设置

        Notification notification = builder.build() ;

        notificationManager.notify(99, notification);
    }


}
